/*

Test SVN

test lock

*/

#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;

#define SCREEN_W 800
#define SCREEN_H 600
#define ENEMY_LIST_SIZE 30
#include "player.h"
#include "enemy.h"

bool collision_check(float Ax, float Ay, float Aw, float Ah, float Bx, float By, float Bw, float Bh)
{

    if ( Ay+Ah < By ) return false; //if A is more to the left than B
    else if ( Ay > By+Bh ) return false; //if A is more to the right than B
    else if ( Ax+Aw < Bx ) return false; //if A is higher than B
    else if ( Ax > Bx+Bw ) return false; //if A is lower than B

    return true; //There is a collision because none of above returned false

}


int main()
{
    sf::RenderWindow window(sf::VideoMode(SCREEN_W, SCREEN_H), "SFML project!");
  //  sf::RenderWindow window2(sf::VideoMode(800, 600), "SFML works!");

    float collision_counter = 0; // debugging tells how many collisions have happened

    float collision_timer = 0; // limits collision checking so it doesnt happen too fast

    int number_coins = 0;

    string print_string1_base = "Coins: ";
    char aChar = '0' + number_coins;
    string print_string1 = print_string1_base + aChar;



    sf::Font font;
    if (!font.loadFromFile("graphics/arial.ttf"))
    {
        cout << "error font not found";
    }

    sf::Text text;

    // select the font
    text.setFont(font); // font is a sf::Font

    // set the string to display
    text.setString(print_string1);

    // set the character size
    text.setCharacterSize(20); // in pixels, not points!

    // set the color
    text.setColor(sf::Color::Yellow);

    // set the text style
    text.setStyle(sf::Text::Bold);


    sf::Texture texture;
    if (!texture.loadFromFile("graphics/ship.png"))
    {
        cout << "error image not found";
    }

    sf::Texture hp_texture;
    if (!hp_texture.loadFromFile("graphics/greenbarSmall.png"))
    {
        cout << "error image not found";
    }

    sf::Sprite hp_sprite;
    hp_sprite.setTexture(hp_texture);


    sf::Texture hp_bar_texture;
    if (!hp_bar_texture.loadFromFile("graphics/barContain.png"))
    {
        cout << "error image not found";
    }
    sf::Sprite hp_bar_sprite;
    hp_bar_sprite.setTexture(hp_bar_texture);

    sf::Texture red;
    if (!red.loadFromFile("graphics/redsmall.png"))
    {
        cout << "error image not found";
    }

    sf::Texture coin;
    if (!coin.loadFromFile("graphics/coin.png"))
    {
        cout << "error image not found";
    }

    sf::Texture health_pack;
    if (!health_pack.loadFromFile("graphics/health_pack.png"))
    {
        cout << "error image not found";
    }


    player boxone(100,25,25,25); // spawn x&y, object width and height
    // psprite must be set here, so that the texture pointer isnt lost in memory
    // perhaps a function could be called to do this? so that it doesnt take up a ton of room in the source code
    boxone.psprite.setTexture(texture);

    // it would be best if the image could be handled inside the object's class


    player box2(0,100,284,177);
  //  box2.psprite.setTexture(texture);
   box2.giveImage(texture);


// create array of enemy objects
   enemy enemy_list[ENEMY_LIST_SIZE];

// initialize values for enemies
    for (int i=0; i < ENEMY_LIST_SIZE; i++)
    {

    int num = i * 30.0;
    // create object
    if (i % 5 == 0)
        enemy_list[i].set_values(0, num, 50, 50, i/40.0, 0, "health_pack");
    else if (i % 2 == 0 && i != 0)
        enemy_list[i].set_values(0, num, 25, 25, i/40.0, 1, "enemy");
    else if (i % 3 == 0)
        enemy_list[i].set_values(0, num, 17, 17, i/40.0, 0, "coin");

    else
        enemy_list[i].set_values(0, num, 25, 25, .03, 1, "enemy");

    // set sprite based on what type of object
    if (enemy_list[i].object_type == "enemy")
        enemy_list[i].psprite.setTexture(red);
    else if (enemy_list[i].object_type == "coin")
        enemy_list[i].psprite.setTexture(coin);
    else if (enemy_list[i].object_type == "health_pack")
        enemy_list[i].psprite.setTexture(health_pack);


    }




    sf::View view1;


    // Main Loop
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear(sf::Color::Black); // window color
        //window.draw(shape);

// camera settings
        view1.setCenter(boxone.box_x, boxone.box_y);
        view1.setSize(sf::Vector2f(400, 300));
        window.setView(view1);
// end camera settings

        window.draw(boxone.psprite);
        boxone.updateMe();

    // update  draw enemies
    for (int i=0; i < ENEMY_LIST_SIZE; i++)
    {

        if (enemy_list[i].alive)
        {

            window.draw(enemy_list[i].psprite);
            enemy_list[i].updateMe();

            // check for collisions
            if (collision_check(boxone.box_x, boxone.box_y, boxone.box_w, boxone.box_h, enemy_list[i].box_x, enemy_list[i].box_y,enemy_list[i].box_w, enemy_list[i].box_h))
            {
                //collision_counter++;
                //cout << "collision " << collision_counter << endl ; // debug

                if (enemy_list[i].object_type == "enemy")
                {
                    // if collision player loses hp
                    boxone.hp -= 5;
                }
                else if (enemy_list[i].object_type == "coin")
                {
                    cout << "coin collected!!" << endl;
                    number_coins++;
                }
                else if (enemy_list[i].object_type == "health_pack")
                {
                    cout << "health_pack collected!!" << endl;
                    boxone.hp += 5;
                }

                enemy_list[i].alive = false;

            }

        }

    }

// draw the hp bar
        window.draw(hp_bar_sprite);
        hp_bar_sprite.setPosition(sf::Vector2f(boxone.box_x - 200, boxone.box_y - 150));
        hp_bar_sprite.setScale(sf::Vector2f(1.0f, 1.0f));

        window.draw(hp_sprite);
        hp_sprite.setPosition(sf::Vector2f(boxone.box_x - 199, boxone.box_y - 149));
        hp_sprite.setScale(sf::Vector2f(boxone.hp / boxone.fullhp, 1.0f));

// end hp bar

        aChar = '0' + number_coins;
        print_string1 = print_string1_base + aChar;
        text.setString(print_string1);
        window.draw(text);
        text.setPosition(sf::Vector2f(boxone.box_x - 200, boxone.box_y - 135));

        window.display();
    } // end main loop

    return 0;
}
